package com.vcaresolution.logistic.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.DatemanagerForm;
import com.vcaresolution.logistic.to.DatemanagerTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>DateManagerController</code> is used to track all login related actions
 *
 * @author Dharam C. Gusai
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class DatemanagerController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.DATEMANAGERFORM_MODELATTRIBUTE)
	public DatemanagerForm createForm(){
		return new DatemanagerForm();
	}
	
	private void loadRequiredDetailInModelObject(ModelAndView modelAndView){
		modelAndView.addObject(ApplicationConstants.JOBREGISTERLIST_MODELATTRIBUTE, getServiceRegistry().getJobRegisterService().findByAll());
		
		modelAndView.addObject(ApplicationConstants.VESSELLIST_MODELATTRIBUTE, getServiceRegistry().getVesselService().findByAll());
//		modelAndView.addObject(ApplicationConstants.PORTLIST_MODELATTRIBUTE, getServiceRegistry().getPortService().findByAll());
//		modelAndView.addObject(ApplicationConstants.CONTACTLIST_MODELATTRIBUTE, getServiceRegistry().getContactService().findByAll());
//		
//		modelAndView.addObject(ApplicationConstants.MODELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("MODE"));
//		modelAndView.addObject(ApplicationConstants.CATEGORYLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("JRCATEGORY"));
//		modelAndView.addObject(ApplicationConstants.CARGOTYPELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("CARGOTYPE"));
//		modelAndView.addObject(ApplicationConstants.STUFFBYLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("STUFFBY"));		
//		modelAndView.addObject(ApplicationConstants.ADDITIONALREQUIREMENTSLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("ADDITIONALREQUIRMENTS"));
//		modelAndView.addObject(ApplicationConstants.HAZCLASSLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("HAZCLASS"));
//		modelAndView.addObject(ApplicationConstants.HAZPKGGROUPLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("PKGGROUP"));
	}
	
	/**
	 * This method is called when the GET request has been made to the Add DateManager Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_FCLDATEMANAGER_URL,method = RequestMethod.GET)
	public ModelAndView loadAddDateManager() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(ApplicationConstants.ADD_FCLDATEMANAGER_VIEW);
		modelAndView.addObject(ApplicationConstants.DATEMANAGERFORM_MODELATTRIBUTE, new DatemanagerForm());
		
		loadRequiredDetailInModelObject(modelAndView);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADD DateManager Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_FCLDATEMANAGER_URL,method = RequestMethod.POST)
	public ModelAndView processAddDateManager(@Valid DatemanagerForm dateManagerForm, 
			 final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			
			dateManagerForm.setUser(getLoggedInUser(request));
			getServiceRegistry().getDatemanagerService().add(dateManagerForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_FCLDATEMANAGER_URL, true, true, false));
		} else {
			
			loadRequiredDetailInModelObject(modelAndView);
		}		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the List DateManager Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_FCLDATEMANAGER_URL,method = RequestMethod.GET)
	public ModelAndView list() {
		final ModelAndView modelAndView = new ModelAndView();
		
		List<DatemanagerTO> datemanagerList = getServiceRegistry().getDatemanagerService().findByAll();
		
		modelAndView.addObject(ApplicationConstants.DATEMANAGERLIST_MODELATTRIBUTE, datemanagerList);
		modelAndView.setViewName(ApplicationConstants.LIST_FCLDATEMANAGER_VIEW);
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ADD DateManager Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_FCLDATEMANAGER_URL,method = RequestMethod.GET)
	public ModelAndView loadEditDateManager(@RequestParam(value="dmId") String dmId) {
		ModelAndView modelAndView = new ModelAndView();
		
		Object datemanagerObject = getServiceRegistry().getDatemanagerService().findById(Integer.parseInt(dmId));
		loadRequiredDetailInModelObject(modelAndView);
		
		modelAndView.addObject(ApplicationConstants.DATEMANAGERFORM_MODELATTRIBUTE, datemanagerObject);
		modelAndView.setViewName(ApplicationConstants.EDIT_FCLDATEMANAGER_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADD DateManager Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_FCLDATEMANAGER_URL,method = RequestMethod.POST)
	public ModelAndView processEditDateManager(@Valid DatemanagerForm dateManagerForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			
			dateManagerForm.setUser(getLoggedInUser(request));
			getServiceRegistry().getDatemanagerService().update(dateManagerForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_FCLDATEMANAGER_URL, true, true, false));
		} 
		else {	
			loadRequiredDetailInModelObject(modelAndView);	
		}		
		return modelAndView;
	}
}